<template>
  <div class="login-container">
    <div class="Loginleft">
      <h3 class="LLtitle">短链监控</h3>
      <img src="@/assets/loginBGc.png" alt="" style="width: 900px;height: 900px;margin-left: 100px;">
    </div>
    <div class="LoginBox">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
        <div class="LBrBox">
          <h2 class="lbrboxx1">登录</h2>
          <h2 class="lbrboxx2">欢迎登录短链平台</h2>
        </div>

        <el-form-item prop="username">
          <div class="riloicon">
            <img src="@/assets/404_images/loginicon2.png" alt="" class="riloimg">
            <span class="rilotitle">账号</span>
          </div>
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text"
            tabindex="1"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <div class="riloicon">
            <img src="@/assets/404_images/loginicon3.png" alt="" class="riloimg">
            <span class="rilotitle">密码</span>
          </div>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
            placeholder="请输入密码" name="password" tabindex="2" @keyup.enter.native="handleLogin" show-password></el-input>

        </el-form-item>
        <el-form-item prop="password">
          <div class="riloicon">
            <img src="@/assets/404_images/loginicon1.png" alt="" class="riloimg">
            <span class="rilotitle">验证码</span>
          </div>
          <div class="DenifyDivBox">
            <div style="width: 100%;">
              <el-input type="text" v-model="DenfityCode" auto-complete="off" placeholder="验证码"></el-input>
            </div>
            <div @click="refreshCode" class="RidentifyBox">
              <Denfity :identifyCode="identifyCode"></Denfity>

            </div>
          </div>
        </el-form-item>
        <el-button :loading="loading" type="primary" class="LoginButton"
          @click.native.prevent="handleLogin">登录</el-button>


      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";
import {
  validUsername
} from "@/utils/validate";
import Denfity from '../../components/dentify.vue'
export default {
  name: "Login",
  components: {
    Denfity,
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      callback();
      // if (!validUsername(value)) {
      //   callback(new Error("Please enter the correct user name"));
      // } else {
      //   callback();
      // }
    };
    const validatePassword = (rule, value, callback) => {
      callback();
      // if (value.length < 6) {
      //   callback(new Error("The password can not be less than 6 digits"));
      // } else {
      //   callback();
      // }
    };
    return {
      DenfityCode: '',
      identifyCode: '',
      identifyCodes: "23456789abcdefghjkmnpqrstuvwxyz",
      loginForm: {
        username: "",
        password: "",
      },
      yzm: '',
      loginRules: {
        username: [{
          required: true,
          trigger: "blur",
          validator: validateUsername
        },],
        password: [{
          required: true,
          trigger: "blur",
          validator: validatePassword
        },],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  mounted() {
    // 初始化验证码
    this.identifyCode = "";
    //参数：（1）随机串内容。（2）验证码显示位数
    this.makeCode(this.identifyCodes, 4);
  },
  methods: {

    // showPwd() {
    //   if (this.passwordType === "password") {
    //     this.passwordType = "";
    //   } else {
    //     this.passwordType = "password";
    //   }
    //   this.$nextTick(() => {
    //     this.$refs.password.focus();
    //   });
    // },
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        //通过循环获取字符串内随机几位
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];
      }
      this.DenfityCode = ''
    },
    //随机数字：用于当角标拿字符串的值
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          if (this.identifyCode == this.DenfityCode) {
            // this.loading = true;

            // login(this.loginForm).then(res => {
            //   if (res.code === 200) {
            //         this.$router.push({
            //       path: this.redirect || "/"
            //     });
            //     this.loading = false;
            //     this.$message({
            //       message: res.msg,
            //       type: 'success',
            //       duration: 1000
            //     });
            //   }else{

            //     this.$message({
            //       message: res.data,
            //       type: 'error',
            //       duration: 1000
            //     });
            //     this.loading = false;

            //   }

            // })
            
            this.$store
              .dispatch("user/login", this.loginForm)
              .then(() => {
                this.$router.push({
                  path: this.redirect || "/"
                });
                this.loading = false;
                this.$message({
                  message: "登录成功",
                  type: 'success',
                  duration: 1000
                });
              })
              .catch((error) => {
                this.loading = false;
                this.$message({
                  message: error,
                  type: 'error',
                  duration: 1000
                });
              });
          } else {
            this.$message({
              message: '验证码错误',
              type: 'error',
              duration: 1000
            });
          }
        } else {
          console.log("error submit!!");
          return false;

        }
      });
    },
  },
};
</script>



<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .Loginleft {
    width: 60%;
    height: 100%;
    background-color: #fff;

    .LLtitle {
      font-family: zcool-gdh, zcool-gdh;
      font-weight: 600;
      font-size: 70px;
      color: #0052AE;
      margin: 30px 0 0 30px;
    }
  }

  .LoginBox {
    width: 40%;
    height: 100%;
    padding: 30px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    background-color: #0052AE;
  }

  .LBrBox {
    .lbrboxx1 {
      font-family: HelloFont WenYiHei, HelloFont WenYiHei;
      font-weight: 600;
      font-size: 42px;
      color: #FFFFFF;
    }

    .lbrboxx2 {
      font-family: HelloFont WenYiHei, HelloFont WenYiHei;
      font-weight: 600;
      font-size: 32px;
      color: #FFFFFF;
    }
  }

  .riloicon {
    display: flex;
    align-items: center;
    margin-bottom: 10px;

    .riloimg {
      width: 30px;
      height: 30px;
    }

    .rilotitle {
      font-size: 20px;
      color: #FFFFFF;
      margin: 0 3px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }



  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.DenifyDivBox {
  display: flex;
  align-items: center;
  // justify-content: space-between;
}

.RidentifyBox {
  height: 40px !important;
  display: flex;
  height: auto;
}

.LoginButton {
  width: 100%;
  margin: 50px 0 0 0;
  background-color: #fff;
  color: #0052AE;
  font-size: 24px;
}

::v-deep .el-input__icon {
  font-size: 20px;
  color: #70BBFF;
}


::v-deep .el-input__inner {
  background-color: #0052AE;
  border: 1px solid #0052AE;
  border-bottom: 1px solid #fff;
  color: #fff;
}


</style>
